package com.rxkj.service.impl;

import com.rxkj.entity.gnss.*;
import com.rxkj.mapper.GnssDataMapper;
import com.rxkj.service.GnssDataService;
import com.rxkj.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GnssDataServiceImpl implements GnssDataService {

    @Autowired
    private GnssDataMapper gnssDataMapper;

    /**
     * todo type: 1. 位移，2. 倾角，3. 加速度
     * todo MONITORDEVICECODE: 监测类型编号
     * @param queryGnss
     * @return
     */
    @Override
    public PageData<GnssDataVO> getGnssDataPage(QueryGnssVO queryGnss) {
        Long totalRows = gnssDataMapper.getTotalRows(queryGnss);
        PageData<GnssDataVO> res = new PageData<GnssDataVO>(queryGnss.getCurrentPage(), queryGnss.getPageSize(), totalRows);
        List<GnssDataVO> qe = gnssDataMapper.getGnssDataPage(res.getStart(), res.getEnd(), queryGnss);
        res.setRows(qe);
        return res;
    }

    @Override
    public Map<String, Object> getSelectDownData(String areaCode) {
        Map<String, Object> res = new HashMap<>();
        List<HiddenPointVO> hiddenPoint = gnssDataMapper.getHiddenPointByAreaCode(areaCode);
        List<SectionVO> section = gnssDataMapper.getSectionByAreaCode(areaCode);
        List<GnssDeviceVO> device = gnssDataMapper.getDeviceByAreaCode(areaCode);
        res.put("hiddenPoint", hiddenPoint);
        res.put("section", section);
        res.put("device", device);
        return res;
    }

    @Override
    public PageData<GnssDataVO> initDevDataPage(int currentPage, int pageSize, String deviceName) {
        Long totalRows = gnssDataMapper.initDevDataTotalRows("510521", deviceName);
        PageData<GnssDataVO> res = new PageData<GnssDataVO>(currentPage, pageSize, totalRows);
        //查出本页设备id，再遍历查询最新一条数据，这样查的快
        List<DeviceIDVO> devs = gnssDataMapper.getCurrentPageDevIds("510521", res.getStart(), res.getEnd(), deviceName);
        List<GnssDataVO> qe = new ArrayList<>();
        devs.forEach(dev -> {
            System.out.println("dev: " + dev);
            GnssDataVO gdv = gnssDataMapper.getGnssDataByDevid(dev.getDeviceId());
            if (gdv == null){
                gdv = new GnssDataVO();
                gdv.setDeviceId(dev.getDeviceId());
            }
            gdv.setDeviceName(dev.getDeviceName());
            gdv.setMainDeviceId(dev.getMainDeviceId());
            gdv.setYhdName(dev.getYhdName());
            gdv.setAreaName(dev.getAreaName());
            qe.add(gdv);
        });
        res.setRows(qe);
        return res;
    }

    @Override
    public PageData<GnssDataVO> getDevDataPage(QueryGnssVO queryGnss) {
        Long totalRows = gnssDataMapper.getDevDataTotalRows(queryGnss);
        PageData<GnssDataVO> res = new PageData<GnssDataVO>(queryGnss.getCurrentPage(), queryGnss.getPageSize(), totalRows);
        List<GnssDataVO> qe = gnssDataMapper.getDevDataPageByDeviceid(res.getStart(), res.getEnd(), queryGnss);
        res.setRows(qe);
        return res;
    }

    @Override
    public List<GnssDataVO> getGnssChartDataByDveId(String startTime, String endTime, String deviceId) {
        return gnssDataMapper.getGnssChartDataByDveId(startTime, endTime, deviceId);
    }


}
